package reesoft.common.entity;

import reesoft.common.util.ClassUtil;
import reesoft.common.util.EncryptUtil;

import java.util.Map;

/**
 * 业务请求基类
 *
 * Created by reesoft on 2017-08-29
 */
public class BaseRequest
{
    // 命令字
    private String command;

    // 发起请求的系统标识
    private String appId;

    // 发起请求的系统内唯一的请求序号
    private Long requestId;

    public String getCommand()
    {
        return command;
    }

    public void setCommand(String command)
    {
        this.command = command;
    }

    public String getAppId()
    {
        return appId;
    }

    public void setAppId(String appId)
    {
        this.appId = appId;
    }

    public Long getRequestId()
    {
        return requestId;
    }

    public void setRequestId(Long requestId)
    {
        this.requestId = requestId;
    }

    public Map<String, String> toParameterMap()
    {
        return ClassUtil.toAttributeMap(this, this.getClass(), null);
    }

    /**
     * 用对象的所有属性成员生成 URL 参数字符串
     *
     * @return URL 参数字符串，包含 ? 字符
     */
    public String toParameterString()
    {
        Map<String, String> parameters = toParameterMap();

        StringBuilder result = new StringBuilder("?");

        for (String key : parameters.keySet())
        {
            String value = parameters.get(key);

            if (value != null)
            {
                result.append(key).append("=").append(EncryptUtil.urlEncode(value)).append("&");
            }
        }

        if (result.length() > 1)
        {
            result.delete(result.length() - 1, result.length());
        }

        return result.toString();
    }
}
